package com.cskaoyan.service.wxcollect;

import com.cskaoyan.bean.MallCollect;
import com.cskaoyan.bean.MallCollectExample;
import com.cskaoyan.bean.MallGoods;
import com.cskaoyan.mapper.MallCollectMapper;
import com.cskaoyan.mapper.MallGoodsMapper;
import com.cskaoyan.model.bo.wxcollect.AddordeleteBo;
import com.cskaoyan.model.vo.BaseData;
import com.cskaoyan.model.vo.wxcollect.CollectVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @program: project-market
 * @description:
 * @author: Weiwei Liu
 * @create: 2021-12-02 22:29
 **/
@Service
public class WxCollectServiceImpl implements WxCollectService {

    @Autowired
    MallCollectMapper mallCollectMapper;
    @Autowired
    MallGoodsMapper mallGoodsMapper;

    @Override
    public Integer addordelete(AddordeleteBo bo, Integer userId) {

        // 如果已经收藏了就取消收藏，如果没有收藏就加入收藏

        MallCollect mallCollect = mallCollectMapper.selectCollectByUserIdAndValueId(bo.getValueId(), userId);

        if (mallCollect == null) {
            // 说明没有收藏，这时候需要加入收藏
            return mallCollectMapper.insertCollect(bo, userId);
        }
        // 无论有没有被收藏，都把deleted的值取反
        mallCollect.setDeleted(!mallCollect.getDeleted());
        mallCollect.setUpdateTime(new Date());

        return mallCollectMapper.updateByPrimaryKey(mallCollect);

    }

    @Override
    public BaseData list(Integer userId, Integer type, Integer page, Integer limit) {

        PageHelper.startPage(page, limit);

        MallCollectExample example = new MallCollectExample();
        example.createCriteria().andDeletedEqualTo(false).andUserIdEqualTo(userId);

        // 设置分页信息
        example.setOrderByClause("update_time" + " " + "desc");

        List<CollectVo> collectVoArrayLis = new ArrayList<CollectVo>();

        List<MallCollect> list = mallCollectMapper.selectByExample(example);

        for (MallCollect mallCollect : list) {
            Integer valueId = mallCollect.getValueId();
            // 通过value_id查到goods信息
            MallGoods mallGoods = mallGoodsMapper.selectGoodsById(valueId);

            CollectVo collectVo = new CollectVo();
            collectVo.setId(mallCollect.getId());
            collectVo.setBrief(mallGoods.getBrief());
            collectVo.setName(mallGoods.getName());
            collectVo.setPicUrl(mallGoods.getPicUrl());
            collectVo.setRetailPrice(mallGoods.getRetailPrice());
            collectVo.setType(type);
            collectVo.setValueId(mallGoods.getId());
            collectVoArrayLis.add(collectVo);
        }


        //通过分页信息拿到分页数据
        //PageInfo的有参构造方法，放入的***查询结果***
        PageInfo info = new PageInfo(list);

        BaseData vo = new BaseData();

        vo.setTotal((int) info.getTotal());
        vo.setLimit(limit);
        vo.setPage(info.getPageSize());
        vo.setPages(info.getPages());
        vo.setList(collectVoArrayLis);
        return vo;

    }
}
